package worst.shopping.lists;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import worst.shopping.adapter.ImageCursorAdapter;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;

public class EditListHighlightableImageCursorAdapter extends ImageCursorAdapter {

	private Map< Integer, Integer > _selectedItems;
	private Drawable _highlight;
	
	public EditListHighlightableImageCursorAdapter(Context context, int layout,
			Cursor cursor, String[] from, int[] to, Drawable highlight) {
		super(context, layout, cursor, from, to);

		_selectedItems = new HashMap<Integer, Integer>();
		_highlight = highlight;
	}
	
	public void selectItem(int itemid, int count)
	{
		_selectedItems.put(itemid, count);
	}
	
	public void unselectItem(int itemid)
	{
		_selectedItems.remove(itemid);
	}
	
	public boolean isSelected(int itemid)
	{
		return _selectedItems.containsKey(itemid);
	}
	
	public int getCountForSelectedItem(int itemid)
	{
		Integer count = _selectedItems.get(itemid);
		
		if(count == null)
			return 0;
		else
			return count;
	}
	
	@Override
	public View getView(int pos, View inView, ViewGroup parent) {
		inView = super.getView(pos, inView, parent);	
		
		ViewHolder viewHolder = (ViewHolder) inView.getTag();
		
		if(_selectedItems.containsKey(pos))
		{
			inView.setBackgroundDrawable(_highlight);
			
			viewHolder.productName.setTextColor(Color.BLACK);
			viewHolder.productDescription.setTextColor(Color.BLACK);
			viewHolder.productCount.setText(_selectedItems.get(pos).toString());
			viewHolder.productCount.setTextColor(Color.BLACK);
			viewHolder.productPriceTotal.setTextColor(Color.BLACK);
		}
		else
		{
			viewHolder.productName.setTextColor(Color.WHITE);
			viewHolder.productDescription.setTextColor(Color.GRAY);	
			viewHolder.productCount.setTextColor(Color.WHITE);
			viewHolder.productPriceTotal.setTextColor(Color.WHITE);
			inView.setBackgroundColor(Color.TRANSPARENT);
		}
		
		return inView;
	}

	public Set<Integer> getSelectedItemsKeySet() {
		return _selectedItems.keySet();
	}
	

	public void setSelectedItemsEntrySet(Set< Entry <Integer, Integer>> selectedItemsKeySet) {
		for (Entry<Integer, Integer> entry : selectedItemsKeySet) {
			_selectedItems.put(entry.getKey(), entry.getValue());
		}
	}

	public Set< Entry <Integer, Integer>> getSelectedItemsEntrySet() {
		return _selectedItems.entrySet();
	}

}
